package com.symria.server.bo.app;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.symria.framework.page.Page;
import com.symria.framework.util.DateUtil;
import com.symria.framework.util.StringUtil;
import com.symria.server.bo.common.CommonService;
import com.symria.server.dao.app.APPDao;
import com.symria.server.dao.app.AppReportDao;
import com.symria.server.po.AppInfo;
import com.symria.server.po.UserInfo;
import com.symria.system.constants.AppStatus;
import com.symria.system.util.CallPushManager;
import com.symria.system.util.SessionHelper;

public class APPService extends CommonService{
	
	private APPDao appDao;
	private AppReportDao appReportDao;
	
	public void saveAPPInfo(AppInfo appInfo){
		appInfo.setUserId(SessionHelper.getUserInfo().getUserId());
		
		if(appInfo.getPublicId()==null){
			UUID uuid = UUID.randomUUID();
			appInfo.setPublicId(uuid.toString());
		}
		if(appInfo.getSoftPath()==null){
			appInfo.setStatus(AppStatus.STATUS_NORMAL.toString());
		}else{
			appInfo.setStatus(AppStatus.STATUS_WAIT.toString());
		}
		
		appDao.saveOrUpdate(appInfo);
	}
	
	public void updateAppWithAbsence(AppInfo app){
		AppInfo app1=(AppInfo) appDao.getObject(AppInfo.class, app.getAppId());
		if(!StringUtil.isEmpty(app.getAppCate())){
			app1.setAppCate(app.getAppCate());
		}
		if(!StringUtil.isEmpty(app.getAppDesc())){
			app1.setAppDesc(app.getAppDesc());
		}
		if(!StringUtil.isEmpty(app.getAppIco())){
			app1.setAppIco(app.getAppIco());
		}
		if(!StringUtil.isEmpty(app.getAppName())){
			app1.setAppName(app.getAppName());
		}
		if(!StringUtil.isEmpty(app.getAppPlat())){
			app1.setAppPlat(app.getAppPlat());
		}
		if(!StringUtil.isEmpty(app.getMinSdk())){
			app1.setMinSdk(app.getMinSdk());
		}
		if(!StringUtil.isEmpty(app.getPackageName())){
			app1.setPackageName(app.getPackageName());
		}
		if(!StringUtil.isEmpty(app.getSoftPath())){
			app1.setSoftPath(app.getSoftPath());
		}
		if(!StringUtil.isEmpty(app.getStatus())){
			app1.setStatus(app.getStatus());
		}
		if(!StringUtil.isEmpty(app.getSuffix())){
			app1.setSuffix(app.getSuffix());
		}
		if(!StringUtil.isEmpty(app.getUnit())){
			app1.setUnit(app.getUnit());
		}
		if(app.getRate()!=null){
			app1.setRate(app.getRate());
		}
		appDao.updateObject(app1);
	}
	
	public AppInfo findAppInfoById(Integer appId){
		return (AppInfo) appDao.getObject(AppInfo.class, appId);
	}
    
	
	
	public void searchApp(String appName,String appPlat,String appCate,String status,Page page){
		UserInfo userInfo=SessionHelper.getUserInfo();
		appDao.searchApp(appName, appPlat, appCate, status,userInfo.getUserId(),page);
	}
	public void updateAppState(AppInfo app,String state){
		AppStatus s = AppStatus.fromValue(state);
		if(app!=null && s!=null){
			app.setStatus(s.toString());
			updateObject(app);
			CallPushManager.refreshAppCache(app.getPublicId());
		}
	}
	
	
	
	public List<AppInfo> searchApp(){
		UserInfo userInfo=SessionHelper.getUserInfo();
		return appDao.findAllApp(userInfo.getUserId());
	}
	
	public void deleteApp(Integer appId){
		AppInfo appInfo=(AppInfo) appDao.getObject(AppInfo.class, appId);
		appInfo.setStatus(AppStatus.STATUS_DEL.toString());
		appDao.saveOrUpdate(appInfo);
	}
	
	public void searchAppReport(String[] appId,Date beginTime,Date endTime,Page page){
		List<Integer> list=new ArrayList<Integer>();
		
		if(appId!=null){
		for(String s:appId){
			list.add(Integer.valueOf(s));
		}
		}
		
		appReportDao.searchByAPPandTime(list.toArray(new Integer[1]), beginTime, endTime, page);
	}
	
	
	
	public AppInfo findAppByName(String appName){
		if(StringUtil.isEmpty(appName)){
			return null;
		}
		return appDao.findAppByName(appName);
	}
	
	
	
	public List<Object> showIncomeByDays(int days){
		Date today=new Date();
		
		List<Integer> list=new ArrayList<Integer>();
		List<AppInfo> appInfos=searchApp();
	
		for(AppInfo s:appInfos){
			list.add(s.getAppId());
		}
		
		Date time=getDate00(days);
			
		List<Object> sum=appReportDao.searchSumByAPPandTime(list.toArray(new Integer[1]), time);
		
		return sum;
	}
	
	
	public Date getDate00(int day){
		Date date=DateUtil.getAddDayByDay(new Date(), day);
		return DateUtil.get00HourDate(date);
	}
	
	public Date getDate24(int day){
		Date date=DateUtil.getAddDayByDay(new Date(), day);
		return DateUtil.get24HourDate(date);
	}
	
	
	
	
	
	
	public APPDao getAppDao() {
		return appDao;
	}

	public void setAppDao(APPDao appDao) {
		this.appDao = appDao;
	}

	public AppReportDao getAppReportDao() {
		return appReportDao;
	}

	public void setAppReportDao(AppReportDao appReportDao) {
		this.appReportDao = appReportDao;
	}

	
	
}
